package tools;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import entities.Doc;
import entities.Entity;
import entities.Query;
//use a2e index to retrive possible entities
public class LuceneNameRetriever extends EntityRetriever {
	
	private LuceneIndexReader reader;
	
	//dir is the directory for index
	public LuceneNameRetriever(String dir){
		
		this.reader = new LuceneIndexReader(dir);
		
	}

	@Override
	public ArrayList<Entity> getEntities(String query) {
		ArrayList<Entity> list = new ArrayList<Entity>();
		Set<String> ret1 = this.reader.queryAliasIndex(query);
		
		for( String name:ret1 ){
			Entity e = new Entity(name);
			list.add(e);
		}
		
		return list;
	}

	public LuceneIndexReader getReader() {
		return reader;
	}

	public void setReader(LuceneIndexReader reader) {
		this.reader = reader;
	}
	
	public static void main( String[] args){
		QueryParser qp = new QueryParser("/largedata1/cmput696/students/lyao1/data/dev.xml");
		LuceneNameRetriever r = new LuceneNameRetriever("/largedata1/cmput696/luceneIndex/lucene-a2e");
		DocRetriever dr = new DocRetriever();
		
		try {
			List<Query> qs = qp.getQueries();
			int limit = 1;
			
			//output the entity names for the first #limit query
			System.out.println("out put the entity names for the first "+limit+" queries");
			
			for(int i=0;i<limit;i++){
				String query = qs.get(i).getName();
				System.out.println("Query:"+query);
				ArrayList<Entity> es = r.getEntities(query);
				Doc doc = dr.getDocById(qs.get(i).getDocid());
				System.out.println("Doc:"+doc.getText());
				System.out.println("Total="+es.size()+" Entities:");
				for(Entity e: es){
					System.out.println("\t "+e.getName());
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
